package com.dm.oauth2.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.dm.oauth2.entity.*;
import com.dm.oauth2.mapper.*;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 验证用户信息
 * */
@Service
@Transactional
public class SpringDataUserDetailsService implements UserDetailsService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private PermissionRoleMapper permissionRoleMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 根据账号查询用户信息
     * */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> userDtoQueryWrapper = new QueryWrapper<>();
        userDtoQueryWrapper.eq("username",username);
        User userDto = userMapper.selectOne(userDtoQueryWrapper);
        System.out.println("userDto = " + userDto);
        if (StringUtils.isEmpty(userDto)) {
            //如果用户查询不到，则返回空，有Spring Security自动抛出异常
            return null;
        }
        //根据用户的id查询用户的权限
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",userDto.getUsername());
        User user = userMapper.selectOne(userQueryWrapper);
        if (StringUtils.isEmpty(user)) {
            throw new UsernameNotFoundException("该用户不存在");
        }
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id",user.getId());
        List<UserRole> userRoleList = userRoleMapper.selectList(userRoleQueryWrapper);
        List<String> list = new ArrayList<>();
        userRoleList.forEach(e->{
            QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
            roleQueryWrapper.eq("id",e.getRoleId());
            Role role = roleMapper.selectOne(roleQueryWrapper);
            QueryWrapper<PermissionRole> permissionRoleQueryWrapper = new QueryWrapper<>();
            permissionRoleQueryWrapper.eq("role_id",role.getId());
            List<PermissionRole> permissionRoleList = permissionRoleMapper.selectList(permissionRoleQueryWrapper);
            permissionRoleList.forEach(f->{
                Permission permission = permissionMapper.selectById(f.getPermissionId());
                list.add(permission.getCode());
            });
        });
        //将list转换成数组
        String[] permissionArray = new String[list.size()];
        String[] permission = list.toArray(permissionArray);
        //加盐密码  这里权限这里需要一个数组
        return org.springframework.security.core.userdetails.User.withUsername(userDto.getUsername()).password(userDto.getPassword()).authorities(permission).build();
    }
}
